#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简单任务映射管理器 - 内存版本，避免文件I/O阻塞
"""

import time
import logging
from typing import Dict, Optional
from threading import Lock

logger = logging.getLogger(__name__)

class SimpleTaskMappingManager:
    """简单任务映射管理器 - 仅内存存储"""
    
    def __init__(self):
        self.task_mappings: Dict[str, str] = {}  # task_id -> server_url
        self.lock = Lock()
        logger.info("简单任务映射管理器已初始化")
    
    def add_task_mapping(self, task_id: str, server_url: str):
        """添加任务映射"""
        with self.lock:
            self.task_mappings[task_id] = server_url
            logger.info(f"任务映射已添加: {task_id} -> {server_url}")
    
    def get_task_server(self, task_id: str) -> Optional[str]:
        """获取任务对应的服务器URL"""
        with self.lock:
            return self.task_mappings.get(task_id)
    
    def remove_task_mapping(self, task_id: str):
        """移除任务映射"""
        with self.lock:
            if task_id in self.task_mappings:
                del self.task_mappings[task_id]
                logger.info(f"任务映射已移除: {task_id}")
    
    def get_mapping_stats(self) -> Dict:
        """获取映射统计信息"""
        with self.lock:
            return {
                'total_mappings': len(self.task_mappings),
                'mappings': dict(self.task_mappings)
            }

# 全局实例
simple_task_mapping = SimpleTaskMappingManager()
